package cn.bluedot.tea.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bluedot.framework.annotations.Dispatcher;
import cn.bluedot.framework.annotations.RequestView;
import cn.bluedot.framework.annotations.ResponseJson;
import cn.bluedot.framework.annotations.Service;
import cn.bluedot.framework.service.BaseService;
import cn.bluedot.framework.util.BeanUtil;
import cn.bluedot.framework.web.Result;
import cn.bluedot.tea.dao.BaseDao;
import cn.bluedot.tea.pojo.domain.AdviseRecord;
import cn.bluedot.tea.pojo.domain.User;

@Service
public class AdviseRecordService extends BaseService{
	/**
     * 无参构造函数
     */
    public AdviseRecordService() {

    }

   /**
	 * 有参构造方法
	 * @param params 参数
	 * @param data 存储返回值
	 */
    public AdviseRecordService(String uri,Map<String, Object[]> params, Map<String, Object> data) {
        super(params, data);
        execute(uri,params, data);
    }

   /**
	 * @param params
	 * @param data
	 * 消息通知用例：发送消息
	 * @throws Exception 
	 */
    @RequestView(uri = "/advise/sendAdviseRecord", view = "sendAdviseRecord")//第一个参数为前端请求的URI,第二个参数对应sql.xml里的view的name属性
    @ResponseJson
    public void sendAdviseRecord(Map<String, Object[]> params, Map<String, Object> data) throws Exception {
        //消息通知的业务实现
    	Map<String, Object> param = getParameterMap(params); //将数组值转为单个值
    	Map<String, Object> param2 =new HashMap<String, Object>();
    	param2.put("view", "getUserById");
    	param2.put("userId", param.get("adviseReceviser"));
    	User user=(User)BaseDao.excuteQuery(param2, User.class);
    	if(user==null) {
    		data.put("message", "没有这个工作人员，请重新输入!");
    		data.put("Result", Result.FAILED);
    	}else {
    		param.put("adviseTime", new Date());
    		param.put("adviseStatus", 1);//将这个消息初始设置未1，表示未看
    		int excute=BaseDao.excute(param);
    		if(excute>0) {
    			data.put("message", "消息通知发送成功！等待对方处理");
    			data.put("Result", Result.SUCCESS);
    		}else {
    			data.put("message", "消息发送失败，请重试");
    			data.put("Result", Result.FAILED);
    		}
    	}
    }
    

   /**
	 * @param params
	 * @param data
	 * 消息通知用例：清除消息
	 */
    @RequestView(uri = "/advise/clearUpAdvise", view = "MyAdviseRecord")//第一个参数为前端请求的URI,第二个参数对应sql.xml里的view的name属性
    public void clearAdviseRecord(Map<String, Object[]> params, Map<String, Object> data) {
        //返回消息通知的业务实现
    }
    
    
    /**
     * @param params
     * @param data
     * 消息通知用例：接收消息
     * @throws Exception 
     */
    @RequestView(uri="/advise/myAdvise",view = "receviceAdvise")
    @Dispatcher(success="/message", failed = "/index")
    public void receviceAdviseRecord(Map<String,Object[]> params,Map<String,Object> data) throws Exception{
    	//消息通知的业务实现
    	//查看接收到的通知
    	Map<String, Object> param = getParameterMap(params); //将数组值转为单个值
    		Object receviser=BaseDao.excuteQuery(param, AdviseRecord.class);
    		if(receviser instanceof List) {
    			List list1=(List) receviser;
    			data.put("AdviseReceviseList", list1);
            	data.put("AdviseReceviseListSize",list1.size());
        		data.put("RESULT",Result.SUCCESS);
    		}else if(receviser instanceof AdviseRecord){
    			AdviseRecord adviseRecord=(AdviseRecord) receviser;
    			List<AdviseRecord> list2=new ArrayList<>();
    			list2.add(adviseRecord);
    			data.put("AdviseReceviseList", list2);
            	data.put("AdviseReceviseListSize",list2.size());
        		data.put("RESULT",Result.SUCCESS);
    		}else {
    			List<AdviseRecord> list3=new ArrayList<>();
    			data.put("AdviseReceviseList", list3);
            	data.put("AdviseReceviseListSize",list3.size());
            	data.put("RESULT",Result.SUCCESS);
            	data.put("messageRecevise", "暂时还没有收到通知哦!");
    		}
    		
    		//查看自己发送的通知
    		Map<String, Object> param2 = new HashMap<String, Object>(); //将数组值转为单个值
    			param2.put("view", "getSendAdvise");
    			param2.put("adviseSender", param.get("adviseSender"));
    			Object sender=BaseDao.excuteQuery(param2, AdviseRecord.class);
        		if(sender instanceof List) {
        			List list4=(List) sender;
        			data.put("AdviseSendList", list4);
                	data.put("AdviseSendListSize",list4.size());
            		data.put("RESULT",Result.SUCCESS);
        		}else if(sender instanceof AdviseRecord){
        			AdviseRecord adviseRecord=(AdviseRecord) sender;
        			List<AdviseRecord> list5=new ArrayList<>();
        			list5.add(adviseRecord);
        			data.put("AdviseSendList", list5);
                	data.put("AdviseSendListSize",list5.size());
            		data.put("RESULT",Result.SUCCESS);
        		}else {
        			List<AdviseRecord> list6=new ArrayList<>();
        			data.put("AdviseSendList", list6);
                	data.put("AdviseSendListSize",list6.size());
                	data.put("RESULT",Result.SUCCESS);
                	data.put("messageSend", "暂时还没有发送过通知哦!");
        		}
        		Map<String, Object> param3=new HashMap<String, Object>();//将查看的通知的adviseStatus状态设置为2，表示已查看
        		param3.put("view", "updateAdviseStatusById");
        		param3.put("adviseReceviser",param.get("adviseReceviser"));
        		param3.put("adviseStatus", 2);//设置为已查看
        		BaseDao.excute(param3);
        		
    }
    
    
    /**
     * @param params
     * @param data
     * 消息通知用例：为查看的消息通知
     * @throws Exception 
     */
    @RequestView(uri="/advise/getUnreadAdvise",view = "getUnreadAdvise")
    @ResponseJson
    public void getUnreadAdvise(Map<String,Object[]> params,Map<String,Object> data) throws Exception{
    	//消息通知的业务实现
    	Map<String, Object> param = getParameterMap(params); //将数组值转为单个值
    	param.put("adviseStatus", 1);//表示查询未读通知
    	System.out.println(param);
    		Object object=BaseDao.excuteQuery(param, AdviseRecord.class);
    		if(object instanceof List) {
    			List list=(List) object;
            	data.put("AdviseUnreadListSize",list.size());
        		data.put("RESULT",Result.SUCCESS);
    		}else if(object instanceof AdviseRecord){
            	data.put("AdviseUnreadListSize",1);
        		data.put("RESULT",Result.SUCCESS);
    		}else {
            	data.put("RESULT",Result.SUCCESS);
            	data.put("AdviseUnreadListSize",0);
    		}
    }
    
}

